<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - cameras</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				color: #808080;
				font-family:Monospace;
				font-size:13px;
				text-align:center;

				background-color: #000;
				margin: 0px;
				overflow: hidden;
			}

			#info {
				position: absolute;
				top: 0px; width: 100%;
				padding: 5px;
				z-index: 100;
			}

			a {

				color: #0080ff;
			}

			b { color: lightgreen }
		</style>
	</head>
	<body>
		<div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - cameras<br/>
		<b>O</b> orthographic <b>P</b> perspective
		</div>

		<script src="three.js"></script>

		<script>

			var SCREEN_WIDTH = window.innerWidth;
			var SCREEN_HEIGHT = window.innerHeight;
			var aspect = SCREEN_WIDTH / SCREEN_HEIGHT;

			var container, stats;
			var camera, scene, renderer, mesh;
			var cameraRig, activeCamera, activeHelper;
			var cameraPerspective, cameraOrtho;
			var cameraPerspectiveHelper, cameraOrthoHelper;
			var frustumSize = 600;
			var _controls;
			init();
			animate();

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
				renderer.domElement.style.position = "relative";
				container.appendChild( renderer.domElement );

				renderer.autoClear = false;

				scene = new THREE.Scene();

				//

				camera = new THREE.PerspectiveCamera( 100, (SCREEN_WIDTH * 0.5) / (SCREEN_HEIGHT), 10, 200);
				camera.position.set(50,50, 110);

				//  _controls = new THREE.OrbitControls( camera, renderer.domElement );
			    // _controls.enableRotate = false;
				// _controls.target = new THREE.Vector3(50,50,0);

				var cameraHelper = new THREE.CameraHelper( camera );
				cameraHelper.visible = true;
				scene.add( cameraHelper );

				cameraPerspective = new THREE.PerspectiveCamera( 40,  (SCREEN_WIDTH * 0.5) / (SCREEN_HEIGHT * 0.5), 100, 400 );
				cameraPerspective.position.z = -500;

				
				cameraPerspectiveHelper = new THREE.CameraHelper( cameraPerspective );
				cameraPerspectiveHelper.visible = true;
				scene.add( cameraPerspectiveHelper );


				cameraOrtho = new THREE.PerspectiveCamera( 40, (SCREEN_WIDTH * 0.5) / (SCREEN_HEIGHT * 0.5), 100, 400 );
				cameraOrtho.position.z = -900;
				cameraOrthoHelper = new THREE.CameraHelper( cameraOrtho );
				cameraOrthoHelper.visible = true;
				scene.add( cameraOrthoHelper );


				// counteract different front orientation of cameras vs rig
			//	scene.rotation.y = Math.PI;
			//	cameraOrtho.rotation.y = Math.PI;
			//	cameraPerspective.rotation.y = Math.PI;

				cameraRig = new THREE.Group();

				cameraRig.add( cameraPerspective );
				cameraRig.add( cameraOrtho );
				cameraRig.add( camera );

				scene.add( cameraRig );

				//

				mesh = new THREE.Mesh(
					new THREE.BoxGeometry( 100, 100, 1 ),
					new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } )
				);
				mesh.position.z = 0;
				scene.add( mesh );

				var mesh2 = new THREE.Mesh(
					new THREE.SphereBufferGeometry( 100, 16, 8 ),
					new THREE.MeshBasicMaterial( { color: 0x04ff00, wireframe: true } )
				);
				mesh2.position.z = -800;
				scene.add( mesh2 );

				var mesh3 = new THREE.Mesh(
					new THREE.SphereBufferGeometry( 100, 16, 8 ),
					new THREE.MeshBasicMaterial( { color: 0xff00ff, wireframe: true } )
				);

				mesh3.position.z = -1200;
				scene.add( mesh3 );



				// var geometry = new THREE.Geometry();
				//
				// for ( var i = 0; i < 10000; i ++ ) {
				//
				// 	var vertex = new THREE.Vector3();
				// 	vertex.x = THREE.Math.randFloatSpread( 2000 );
				// 	vertex.y = THREE.Math.randFloatSpread( 2000 );
				// 	vertex.z = THREE.Math.randFloatSpread( 2000 );
				//
				// 	geometry.vertices.push( vertex );
				//
				// }
				//
				// var particles = new THREE.Points( geometry, new THREE.PointsMaterial( { color: 0x888888 } ) );
				// scene.add( particles );

			}

			function animate() {

				requestAnimationFrame( animate );

				render();

			}


			function render() {

				//var r = Date.now() * 0.0005;

				// mesh.position.x = 700 * Math.cos( r );
				// mesh.position.z = 700 * Math.sin( r );
				// mesh.position.y = 700 * Math.sin( r );

				// mesh.children[ 0 ].position.x = 70 * Math.cos( 2 * r );
				// mesh.children[ 0 ].position.z = 70 * Math.sin( r );

				// if ( activeCamera === cameraPerspective ) {

				// 	cameraPerspective.fov = 35 + 30 * Math.sin( 0.5 * r );
				// 	cameraPerspective.far = mesh.position.length();
				// 	cameraPerspective.updateProjectionMatrix();

				// 	cameraPerspectiveHelper.update();
				// 	cameraPerspectiveHelper.visible = true;

				// 	cameraOrthoHelper.visible = false;

				// } else {

				// 	cameraOrtho.far = mesh.position.length();
				// 	cameraOrtho.updateProjectionMatrix();

				// 	cameraOrthoHelper.update();
				// 	cameraOrthoHelper.visible = true;

				// 	cameraPerspectiveHelper.visible = false;

				// }

				//cameraRig.lookAt( mesh.position );

				renderer.clear();
				//_controls.update();
				//activeHelper.visible = true;

				renderer.setViewport( 0, 0, SCREEN_WIDTH/2, SCREEN_HEIGHT );
				renderer.render( scene, camera );


				renderer.setViewport( SCREEN_WIDTH/2, 0, SCREEN_WIDTH/2, SCREEN_HEIGHT/2 );
				renderer.render( scene, cameraPerspective );

				// //activeHelper.visible = true;

				renderer.setViewport( SCREEN_WIDTH/2, SCREEN_HEIGHT/2, SCREEN_WIDTH/2, SCREEN_HEIGHT/2 );
				renderer.render( scene, cameraOrtho );


				

				

			}


		</script>

	</body>
</html>
